﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using NCalc;

namespace TEAA
{
    public class LogicalCondition : Condition
    {
        private string leftOperand;
        private string logicalOperator;
        private string rightOperand;

        public LogicalCondition(string leftOperand, string logicalOperator, string rightOperand)
        {
            this.leftOperand = leftOperand;
            this.logicalOperator = logicalOperator;
            this.rightOperand = rightOperand;
        }

        public bool IsSatisfiedByCondition(DataRow dataRow)
        {
            Regex attributeValue = new Regex(ParserRegularExpressions.ATTRIBUTE_VALUE);
            Regex attributeName = new Regex(ParserRegularExpressions.ATTRIBUTE);

            string leftData = "";
            Match leftMatch = attributeValue.Match(leftOperand);
            if (leftMatch.Success)
            {
                string column = attributeName.Match(leftOperand).Value;
                object data;
                try
                {
                    data = dataRow[column];
                }
                catch (Exception)
                {
                    throw new Exception("Columns are not in the same table to satisfy the selection condition!");
                }
                string dataString = "";
                if (data.GetType() == typeof(string) || data.GetType() == typeof(char) || data.GetType() == typeof(DateTime))
                {
                    dataString = "'" + data.ToString() + "'";
                }
                else
                    dataString = data.ToString();
                leftData = dataString;
            }
            else
            {
                leftData = leftOperand;
            }

            string rightData = "";
            Match rightMatch = attributeValue.Match(rightOperand);
            if (rightMatch.Success)
            {
                string column = attributeName.Match(rightOperand).Value;
                object data;
                try
                {
                    data = dataRow[column];
                }
                catch (Exception)
                {
                    throw new Exception("Columns are not in the same table to satisfy the selection condition!");
                }
                string dataString = "";
                if (data.GetType() == typeof(string) || data.GetType() == typeof(char) || data.GetType() == typeof(DateTime))
                {
                    dataString = "'" + data.ToString() + "'";
                }
                else
                    dataString = data.ToString();
                rightData = dataString;
            }
            else
            {
                rightData = rightOperand;
            }

            string expressionString = leftData + logicalOperator + rightData;
            Expression exp = new Expression(expressionString);
            return (bool)exp.Evaluate();
        }
    }
}
